package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueDept;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.result.ResultCodeEnum;
import cn.micro.system.manage.common.utils.DeptHelper;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimDept;
import cn.micro.system.manage.service.SysDimDeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/manage/sysDimDept")
@RequiredArgsConstructor
@Validated
public class SysDimDeptController {

    private final SysDimDeptService sysDimDeptService;


    @GetMapping("detail/{id}")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    public Object detail(@PathVariable Long id){
        return Result.success(sysDimDeptService.getById(id));
    }

    @PostMapping("save")
    @Log(title = "部门管理--新增部门信息表", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniqueDept @RequestBody SysDimDept sysDimDept){
        if (sysDimDept.getParentId() !=0 && sysDimDeptService.getById(sysDimDept.getParentId()) == null){
            return Result.fail().message("所属部门不存在").code(ResultCodeEnum.FAIL.getCode());
        }
        if (!sysDimDeptService.saveDept(sysDimDept)){
            return Result.fail();
        }
        return Result.success();
    }

    @PutMapping("edit")
    @Log(title = "部门管理--修改部门信息表", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @UniqueDept @RequestBody SysDimDept sysDimDept){
        if (!sysDimDeptService.updateById(sysDimDept)){
            return Result.fail();
        }
        return Result.success();
    }

    @DeleteMapping("remove/{id}")
    @Log(title = "部门管理--删除部门信息表", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    public Object remove(@PathVariable Long id){
        sysDimDeptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("listTree")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    public Object listTree(){
        List<SysDimDept> deptList = sysDimDeptService.list();
        return Result.success(DeptHelper.bulidTree(deptList));
    }

    @GetMapping("dropDeptList")
    public Object dropDeptList(){
        List<SysDimDept> deptList = sysDimDeptService.list();
        return Result.success(DeptHelper.bulidDropTree(deptList));
    }

}
